package com.gxa.clientorder.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.clientorder.exception.BusinessException;
import com.gxa.clientorder.pojo.Sendout;
import com.gxa.clientorder.service.SendoutService;
import com.gxa.clientorder.utils.JsonResult;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author FanJiang
 * @Date 2020/11/13
 * @Version 1.0
 */

@RestController
@RequestMapping("/sen")
@Api(value = "执行发货管理的控制器",tags = "发货管理接口")
public class SendoutController {

    @Resource
    SendoutService sendoutService;

    /**
     * 发货接口
     * @return
     */
    @ApiOperation(value="订单发货接口", notes="订单列表页面，发货接口",httpMethod = "post")
    @PostMapping("/senout")
    public Map senout(@RequestBody @ApiParam(
            name = "sendout",value = "发货的pojo信息。传输json（inputer:当前登录用户,addid:收获地址表id，ordnum:订单号，sennum:发货单号）必传",
            required = true) Sendout sendout){

        Integer insert = sendoutService.insert(sendout);
        if (insert == 0){
            throw new BusinessException("发货失败");
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("发货成功");
        return jsonResult.getValues();
    }

    /**
     * 发货列表接口
     * @param page 当前页码
     * @param limit 当前页面数据量
     * @param outnum 发货单流水号
     * @param ordnum 订单号
     * @param username 收货人
     * @param senstate 发货单状态
     * @return json
     */
    @ApiOperation(value="获取订单列表", notes="订单列表页面，分页显示数据接口",httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页显示数量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "outnum", value = "发货单流水号", required = true, dataType = "int"),
            @ApiImplicitParam(name = "ordnum", value = "订单号", required = true, dataType = "string"),
            @ApiImplicitParam(name = "username", value = "收货人", required = true, dataType = "string"),
            @ApiImplicitParam(name = "senstate", value = "发货单状态", required = true, dataType = "bit")
    })
    //权限注解
    @RequiresPermissions("14")
    @PostMapping("/list")
    public Map list(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit,
                    @RequestParam("outnum") String outnum,@RequestParam("ordnum") String ordnum,
                    @RequestParam("username") String username,@RequestParam("senstate") Integer senstate){

        System.out.println("outnum:" + outnum);
        System.out.println("ordnum:" + ordnum);
        System.out.println("username:" + username);
        System.out.println("senstate:" + senstate);
        //分页
        PageHelper.startPage(page,limit);
        //调用数据
        List senlist = sendoutService.senlist(outnum, ordnum, username, senstate);
        PageInfo inof = PageInfo.of(senlist);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("count",inof.getTotal());
        jsonResult.setData(inof.getList());
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    /**
     * 批量发货
     * @param map
     * @return
     */

    @ApiOperation(value="批量发货接口", notes="暂未实现",httpMethod = "post")
    @ApiImplicitParams({})
    //权限注解
    @RequiresPermissions("16")
    @PostMapping("/batch")
    public Map batchsen(@RequestBody Map map ){


        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("系统正在紧急开发该功能，敬请期待吧(#^.^#)");
        return jsonResult.getValues();
    }

}
